package net.goutalk.glcs.module.erpModel.caseErpDevice.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.erpModel.caseErpDevice.dto.AddCaseErpDeviceInspectDto;
import net.goutalk.glcs.module.erpModel.caseErpDevice.dto.CaseErpDeviceInspectDto;
import net.goutalk.glcs.module.erpModel.caseErpDevice.dto.UpdateCaseErpDeviceInspectDto;
import net.goutalk.glcs.module.erpModel.caseErpDevice.entity.CaseErpDeviceInspect;
import net.goutalk.glcs.module.erpModel.caseErpDevice.service.ICaseErpDeviceInspectService;
import net.goutalk.glcs.module.erpModel.caseErpDevice.vo.CaseErpDeviceInspectExportVo;
import net.goutalk.glcs.module.erpModel.caseErpDevice.vo.CaseErpDeviceInspectInfoVo;
import net.goutalk.glcs.module.erpModel.caseErpDevice.vo.CaseErpDeviceInspectVo;
import net.goutalk.glcs.module.system.entity.DictionaryDetail;
import net.goutalk.glcs.module.system.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * <p>
 * 运维巡检【case_erp_device_inspect】 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-10
 */
@RestController
@RequestMapping(GlobalConstant.CASE_ERP_DEVICE+"/caseErpDeviceInspect")
@Api(value = GlobalConstant.CASE_ERP_DEVICE +"/caseErpDeviceInspect", tags = "运维巡检")
@AllArgsConstructor
public class CaseErpDeviceInspectController {

    private ICaseErpDeviceInspectService caseErpDeviceInspectService;

    private IDictionarydetailService dictionarydetailService;

    @GetMapping(value = "/page")
    @ApiOperation("运维巡检分页")
    public R page(CaseErpDeviceInspectDto dto) {
        IPage<CaseErpDeviceInspectVo> page = caseErpDeviceInspectService.selectJoinListPage(ConventPage.getPage(dto), CaseErpDeviceInspectVo.class,
                MPJWrappers.<CaseErpDeviceInspect>lambdaJoin()
                        .distinct()
                        .orderByDesc(CaseErpDeviceInspect::getCreateDate)
                        .eq(ObjectUtil.isNotEmpty(dto.getTypeId()),CaseErpDeviceInspect::getTypeId,dto.getTypeId())
                        .eq(StrUtil.isNotBlank(dto.getName()),CaseErpDeviceInspect::getName,dto.getName())
                        .eq(ObjectUtil.isNotEmpty(dto.getState()),CaseErpDeviceInspect::getState,dto.getState())
                        .selectAs(DictionaryDetail::getName,CaseErpDeviceInspectVo::getTypeName)
                        .select(CaseErpDeviceInspect::getId)
                        .select(CaseErpDeviceInspect.class, x -> VoToColumnUtil.fieldsToColumns(CaseErpDeviceInspectVo.class).contains(x.getProperty()))
                        .leftJoin(DictionaryDetail.class,DictionaryDetail::getId,CaseErpDeviceInspect::getTypeId));

        PageOutput<CaseErpDeviceInspectVo> pageOutput = ConventPage.getPageOutput(page);
        return R.ok(pageOutput);
    }

    @PostMapping
    @ApiOperation(value = "新增运维巡检信息")
    public R add(@Valid @RequestBody AddCaseErpDeviceInspectDto dto) {
        long count = caseErpDeviceInspectService.count(Wrappers.<CaseErpDeviceInspect>query().lambda().eq(CaseErpDeviceInspect::getDeviceId, dto.getDeviceId()));
        if (count > 0) {
            return R.error("此设备已经巡检完成！");
        }
        return R.ok(caseErpDeviceInspectService.add(dto));
    }

    @PutMapping
    @ApiOperation(value = "修改运维巡检信息")
    public R update(@Valid @RequestBody UpdateCaseErpDeviceInspectDto dto) {
        return R.ok(caseErpDeviceInspectService.update(dto));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询巡检信息")
    public R info(@RequestParam Long id) {
        CaseErpDeviceInspect caseErpDeviceInspect = caseErpDeviceInspectService.getById(id);
        if (caseErpDeviceInspect == null) {
            R.error("找不到此巡检信息！");
        }
        CaseErpDeviceInspectInfoVo caseErpDeviceInspectInfoVo = BeanUtil.toBean(caseErpDeviceInspect, CaseErpDeviceInspectInfoVo.class);
        if (ObjectUtil.isNotEmpty(caseErpDeviceInspectInfoVo.getNatureId())){
             DictionaryDetail byId = dictionarydetailService.getById(caseErpDeviceInspectInfoVo.getNatureId());
             if (StrUtil.isNotBlank(byId.getName())){
                 caseErpDeviceInspectInfoVo.setNatureName(byId.getName());
             }
        }
        return R.ok(caseErpDeviceInspectInfoVo);
    }

    @DeleteMapping
    @ApiOperation(value = "删除运维巡检信息")
    @GlcsLog(value = "删除运维巡检信息")
    public R delete(@Valid @RequestBody Long id) {
        return R.ok(caseErpDeviceInspectService.removeById(id));
    }

    @PostMapping(value = "/export")
    @ApiOperation(value = "导出")
    @GlcsLog(value = "导出表单数据")
    public ResponseEntity<byte[]> export() {
        List<CaseErpDeviceInspectExportVo> erpDeviceInspectExportVoList = caseErpDeviceInspectService.selectJoinList(CaseErpDeviceInspectExportVo.class,
                MPJWrappers.<CaseErpDeviceInspect>lambdaJoin()
                        .distinct()
                        .selectAs(DictionaryDetail::getName,CaseErpDeviceInspectExportVo::getTypeName)
                        .select(CaseErpDeviceInspect::getId)
                        .select(CaseErpDeviceInspect.class, x -> VoToColumnUtil.fieldsToColumns(CaseErpDeviceInspectExportVo.class).contains(x.getProperty()))
                        .leftJoin(DictionaryDetail.class,DictionaryDetail::getId,CaseErpDeviceInspect::getTypeId));

        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, CaseErpDeviceInspectExportVo.class).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(erpDeviceInspectExportVoList);
        return R.fileStream(bot.toByteArray(), "运维巡检信息" + ExcelTypeEnum.XLSX.getValue());
    }

}
